package com.example.booksdeal.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.booksdeal.BmobDBO.BooksDetail;
import com.example.booksdeal.BmobDBO.User;
import com.example.booksdeal.R;
import com.example.booksdeal.basic.MyCollectActivity;
import com.example.booksdeal.basic.MyOrderFormActivity;
import com.example.booksdeal.basic.MyPushActivity;
import com.example.booksdeal.basic.MycenterActivity;
import com.example.booksdeal.basic.SettingActivity;
import com.example.booksdeal.util.DBOpenHelper;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;


public class F5Fragment extends Fragment {

    private ImageView touxiang;
    private TextView nicheng, uid;
    private String username,nickname;
    private DBOpenHelper dbOpenHelper;  //声明DBOpenHelper对象
    private LinearLayout geren,mybooks,order,collect,setting;
    private Bundle bundle,bundle1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f5_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SQLiteStudioService.instance().start(getActivity());
        dbOpenHelper = new DBOpenHelper(getActivity(),"db_booksdeal.db",null,1);  //实例化DBOpenHelper对象，用来创建数据库
        bundle = getActivity().getIntent().getExtras();
        username = bundle.getString("username");
        bundle1 = new Bundle();
        bundle1.putString("username",username);
        Initview();
    }

    public void Initview() {
        touxiang = getActivity().findViewById(R.id.touxiang);
        nicheng = getActivity().findViewById(R.id.nicheng);
        uid = getActivity().findViewById(R.id.uid);
        geren = getActivity().findViewById(R.id.geren);
        mybooks = getActivity().findViewById(R.id.mybooks);
        order = getActivity().findViewById(R.id.order);
        collect = getActivity().findViewById(R.id.collect);
        setting = getActivity().findViewById(R.id.setting);

        uid.setText(username);

        BmobQuery<User> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("username", username);
        categoryBmobQuery.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> object, BmobException e) {
                if (e == null) {
                    nickname = object.get(0).getNickname();
                }
            }
        });


//        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
//        Cursor cursor = db.query("UserInfo", null, "username=?", new String[]{username}, null, null, null);
//        while (cursor.moveToNext()) {
//            nickname = cursor.getString(cursor.getColumnIndex("nickname"));
//        }
        nicheng.setText(nickname);

        geren.setOnClickListener(this::onClick);
        mybooks.setOnClickListener(this::onClick);
        order.setOnClickListener(this::onClick);
        collect.setOnClickListener(this::onClick);
        setting.setOnClickListener(this::onClick);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.geren:
                Intent intent1 = new Intent(getActivity(), MycenterActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                intent1.putExtras(bundle);
                startActivity(intent1);
                break;
            case R.id.mybooks:
                Intent intent2 = new Intent(getActivity(), MyPushActivity.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                intent2.putExtras(bundle);
                startActivity(intent2);
                break;
            case R.id.order:
                Intent intent3 = new Intent(getActivity(), MyOrderFormActivity.class);
                intent3.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                intent3.putExtras(bundle);
                startActivity(intent3);
                break;
            case R.id.collect:
                Intent intent4 = new Intent(getActivity(), MyCollectActivity.class);
                intent4.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                intent4.putExtras(bundle);
                startActivity(intent4);
                break;
            case R.id.setting:
                Intent intent5 = new Intent(getActivity(), SettingActivity.class);
                intent5.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                intent5.putExtras(bundle);
                startActivity(intent5);
                break;
            default:
                break;
        }
    }

}
